package com.castle.fortress.admin.cms.fragment;

import cn.hutool.core.util.StrUtil;
import com.castle.fortress.admin.cms.dto.CmsChannelDto;
import com.castle.fortress.admin.cms.service.CmsChannelService;
import com.castle.fortress.admin.utils.SpringUtils;
import com.castle.fortress.common.utils.ConvertUtil;
import org.beetl.core.tag.GeneralVarTagBinding;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * cms 栏目/导航标签
 * @author castle
 */
public class CmsChannelTag extends GeneralVarTagBinding {

	@Override
	public void render() {
		Map<String,Object> map = new HashMap<>();
		//父级编码
		if(containHtmlAttribute("showflag")){
			try {
				Integer showflag = Integer.valueOf(getAttributeValue("showflag").toString());
				map.put("showflag",showflag);
			} catch (NumberFormatException e) {
				e.printStackTrace();
				map.put("showflag",1);
			}
		}else{
			map.put("showflag",1);
		}
		//父级编码
		if(containHtmlAttribute("pcode")){
			map.put("pcode",getAttributeValue("pcode"));
		}
		//指定栏目编码
		if(containHtmlAttribute("code")){
			map.put("code",getAttributeValue("code"));
		}
		//过滤栏目数目
		if(containHtmlAttribute("row")&&StrUtil.isNotEmpty(getAttributeValue("row").toString())){
			try {
				Integer rows = Integer.valueOf(getAttributeValue("row").toString());
				map.put("row",rows);
			} catch (NumberFormatException e) {
				e.printStackTrace();
			}
		}
		//数目过滤起始
		if(containHtmlAttribute("start") &&StrUtil.isNotEmpty(getAttributeValue("start").toString())){
			try {
				Integer start = Integer.valueOf(getAttributeValue("start").toString());
				map.put("start",start);
			} catch (NumberFormatException e) {
				e.printStackTrace();
			}
		}
		CmsChannelService channelService = SpringUtils.getBean(CmsChannelService.class);
		List<CmsChannelDto> list = ConvertUtil.listToTree(channelService.cmsTagListCmsChannel(map));
		//指定了选中样式
		if(containHtmlAttribute("activeclass")&&containHtmlAttribute("topcode")){
			for(CmsChannelDto d:list){
				if(d.getCode().equals(getHtmlAttribute("topcode").toString())){
					d.setActiveClass(getHtmlAttribute("activeclass").toString());
				}
			}
		}
		this.binds(list);
		this.doBodyRender();
	}
}
